import { ILoginAble } from "../ILoginAble";
import { argType } from "../IPublisher";

export class WXLogin extends ILoginAble {
    protected _wx:any;
    protected _code:string;

    init(type: string): ILoginAble {
        this._wx = window['wx'];
        return super.init(type);
    }

    isSupport(): boolean {
        return true;
    }

    login(caller?: any, onSuccess?: Function, onFail?: Function): void {
        this._wx.login({
            success: (res:{code:string})=>{
                if(res.code){
                    this._code = res.code;
                    // this._wx.getUserInfo({success:(res)=>{
                    //     this._userInfo = res;
                    // }})
                    onSuccess && onSuccess.apply(caller);
                }else{
                    onFail && onFail.apply(caller);
                }
            },
            fail: ()=>{
                onFail && onFail.apply(caller);
            }
        })
    }

    get sessionID():argType {
        return this._code;
    }
}
